﻿using MudBlazor.Utilities;
using MXERP.Blazor.Interop;

namespace MXERP.Blazor.Components
{
    public partial class PopConfirm : MudComponentBase, IOutsideEventJsCallback
    {
        private Origin _anchorOrigin;
        private Origin _transformOrigin;
        [Inject]
        private OutsideClickJsInterop OutsideClickJsInterop { get; set; } = default!;
        [EditorRequired, Parameter]
        public string Title { get; set; } = string.Empty;
        [Parameter]
        public RenderFragment? ChildContent { get; set; }
        [Parameter]
        public EventCallback<MouseEventArgs> PositiveClick { get; set; }
        [Parameter]
        public EventCallback<MouseEventArgs> NegativeClick { get; set; }
        [Parameter]
        public Placement Placement { get; set; } = Placement.Bottom;
        [Parameter]
        public bool Arrow { get; set; } = true;
        [CascadingParameter(Name = "RightToLeft")]
        public bool RightToLeft { get; set; }
        public MudPopover Popover { get; set; } = default!;
        public ElementReference Element { get; set; }
        private bool _open;

        protected string Classname => 
            new CssBuilder("md-popconfirm")
            .AddClass($"md-popconfirm-{ConvertPlacement().ToDescriptionString()}")
            .AddClass("md-popconfirm-arrow", Arrow)
            .AddClass(Class)
            .Build();

        private async Task Confirm(MouseEventArgs e)
        {
            _open = false;
            await PositiveClick.InvokeAsync(e);
        }
        private async Task Cancel(MouseEventArgs e)
        {
            _open = false;
            await NegativeClick.InvokeAsync(e);
        }
        private async Task Open()
        {
            await OutsideClickJsInterop.Initialize(this, Element, Popover.Id);
            _open = !_open;
        }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);
        }

        public Task HandleOnOutsideEventAsync()
        {
            _open = false;
            StateHasChanged();
            return Task.CompletedTask;
        }

        private Origin ConvertPlacement()
        {
            if (Placement == Placement.Bottom)
            {
                _anchorOrigin = Origin.BottomCenter;
                _transformOrigin = Origin.TopCenter;

                return Origin.BottomCenter;
            }

            if (Placement == Placement.Top)
            {
                _anchorOrigin = Origin.TopCenter;
                _transformOrigin = Origin.BottomCenter;

                return Origin.TopCenter;
            }

            if (Placement == Placement.Left || (Placement == Placement.Start && !RightToLeft) || (Placement == Placement.End && RightToLeft))
            {
                _anchorOrigin = Origin.CenterLeft;
                _transformOrigin = Origin.CenterRight;

                return Origin.CenterLeft;
            }

            if (Placement == Placement.Right || (Placement == Placement.End && !RightToLeft) || (Placement == Placement.Start && RightToLeft))
            {
                _anchorOrigin = Origin.CenterRight;
                _transformOrigin = Origin.CenterLeft;

                return Origin.CenterRight;
            }

            return Origin.BottomCenter;
        }
    }
}
